package net.siufung.simple.demo.controller;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.siufung.core.model.enums.MethodEventEnum;
import net.siufung.core.result.Result;
import net.siufung.simple.demo.Event1;
import net.siufung.simple.demo.service.DemoAsyncService;
import net.siufung.simple.demo.service.DemoEventBus;
import net.siufung.web.aop.annotation.AutoIdempotent;
import net.siufung.web.aop.annotation.AutoLimiter;
import net.siufung.web.aop.annotation.AutoLog;
import net.siufung.web.aop.annotation.AutoPermit;
import net.siufung.web.oshi.ServerInfo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.CompletableFuture;

/**
 * @author 陈建峰
 * @since 2022/7/26 10:44 下午
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping(value = "/demo")
public class DemoController {

    private final DemoAsyncService demoAsyncService;
    private final DemoEventBus demoEventBus;

    @AutoIdempotent
    @GetMapping("doTest")
    public String doTest(@RequestParam("name") String name) throws InterruptedException {
        log.info("入参 name={}",name);
        testTrace();
        log.info("调用结束 name={}",name);
        CompletableFuture<String> demoStr = demoAsyncService.demo();
        return "Hello,"+name;
    }

    private void testTrace(){
        log.info("这是一行info日志");
        log.error("这是一行error日志");
        testTrace2();
    }

    private void testTrace2(){
        log.info("这也是一行info日志");
    }

    @GetMapping("doEvent")
    public void doEvent() throws InterruptedException {
        Event1 event1 = new Event1();
        event1.setName("1");
        demoEventBus.post(event1);
    }

    @AutoLimiter(perSecond = 1)
    @AutoPermit("@permit.has('sys_dept_add')")
    @AutoLog(value = "查看服务器资源", methodEvent = MethodEventEnum.QUERY)
    @GetMapping("serverInfo")
    public Result<ServerInfo> serverInfo() throws InterruptedException {
        ServerInfo serverInfo = new ServerInfo();
        serverInfo.copyTo();
        return Result.ok(serverInfo);
    }

}
